package com.jxpanda.spring.module.auth.core.user;

import com.jxpanda.infrastructure.core.tollkit.RegexKit;
import lombok.Getter;

@Getter
public class CaptchaOAuth2User extends AbstractOAuth2User {

    public static final String PHONE_KEY = "phone";
    public static final String EMAIL_KEY = "email";

    private final String identifier;

    private String userIdAttributeName;

    public CaptchaOAuth2User(String userIdAttributeName, String identifier) {
        super(userIdAttributeName);
        this.identifier = identifier;
    }

    @Override
    public String getUserIdAttributeName() {
        // 如果通过配置文件配置了，这个值就不会为null，因此这里只是一个容灾，如果外部没有配置，自动做一次判断
        if (userIdAttributeName == null) {
            if (RegexKit.isEmail(this.identifier)) {
                this.userIdAttributeName = EMAIL_KEY;
            }
            if (RegexKit.isPhone(this.identifier)) {
                this.userIdAttributeName = PHONE_KEY;
            }
            this.attributes.put(AbstractOAuth2User.USER_ID_ATTRIBUTE_NAME, this.userIdAttributeName);
        }
        return userIdAttributeName;
    }

}
